//
//  KSPopUpView.swift
//  ZYA
//
//  Created by 邱仙凯 on 2020/2/1.
//  Copyright © 2020 zwb. All rights reserved.
//

import UIKit
import WebKit
class KSPopUpView: UIView {
    
    
    lazy var webView: WKWebView = {
        let view = WKWebView()
        view.navigationDelegate = self
        view.backgroundColor = UIColor.white
        view.sizeToFit()
        //        view.scalesPageToFit = true
        return view
    }()
    var closeClosure:(()->())?
    var readedClosure:(()->())?
    var titleView: KSPopUpTitleView = {
        let view = KSPopUpTitleView()
        return view
    }()
    
    var readedBtn:KSCaptchaButton = {
        let view = KSCaptchaButton()
        view.layer.cornerRadius = 5
        view.layer.masksToBounds = true
        view.setTitleColor(.white, for: .normal)
        view.setTitle("已阅", for: .normal)
        view.setBackgroundImage(UIImage.from(color: Theme.primaryColor()), for: .normal)
        view.addTarget(self, action: #selector(readedAction), for: .touchUpInside)
        view.maxSecond = 8
        view.setTitle("second秒后点击已阅", for: .disabled)
        view.setBackgroundImage(UIImage.from(color: .lightGray), for: .disabled)
        return view
    }()
    
    init(frame:CGRect,titleStr:String,leftStr:String,rightStr:String,content:String,closeClosure:(()->())?,readedClosure:(()->())?) {
        super.init(frame: frame)
        self.backgroundColor = .white
        self.closeClosure = closeClosure
        self.readedClosure = readedClosure
        
        self.layer.masksToBounds = true
        self.layer.cornerRadius = 12
        
        self.addSubview(webView)
        self.addSubview(titleView)
        self.addSubview(readedBtn)
        
        readedBtn.countdown = true
        
        titleView.btnClosure = {
            if let clo = self.closeClosure {
                clo()
            }
        }
        titleView.snp.makeConstraints { (make) in
            make.top.equalTo(self)
            make.left.equalTo(self)
            make.right.equalTo(self)
            make.height.equalTo(90)
        }
        webView.snp.makeConstraints { (make) in
            make.top.equalTo(self.titleView.snp.bottom)
            make.left.equalTo(self)
            make.right.equalTo(self)
            make.bottom.equalTo(self)
        }
        
        readedBtn.snp.makeConstraints { (make) in
            make.bottom.equalTo(self).offset(-10)
            make.centerX.equalTo(self)
            make.width.equalTo(160)
            make.height.equalTo(32)
        }
        
        webView.loadHTMLString(content, baseURL: URL(string: "http://oa.aonong.com.cn"))
        titleView.titleLabel.text = titleStr
        titleView.leftLabel.text = "\(leftStr)"
        titleView.rightLabel.text = "\(rightStr)"
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    
    @objc func readedAction() {
        if let clo = self.readedClosure {
            clo()
        }
    }
    
}

extension KSPopUpView: WKNavigationDelegate {
    
    func webView(_ webView: WKWebView, didFinish navigation: WKNavigation!) {
        
        /// wkWebView调用js方法
        let js = "document.getElementsByTagName('body')[0].style.webkitTextSizeAdjust= '200%'"
        
        webView.evaluateJavaScript(js) { (response, error) in
            
        }
        
    }
    
}

class KSPopUpTitleView: UIView {
    
    var btnClosure:(()->())?
    
    var titleLabel: UILabel = {
        let label :UILabel = UILabel()
        label.font = UIFont.systemFont(ofSize: CGFloat(17))
        label.textAlignment = .center
        label.numberOfLines = 0
        return label
    }()
    
    var rightLabel: UILabel = {
        let label :UILabel = UILabel()
        label.font = UIFont.systemFont(ofSize: CGFloat(12))
        label.textAlignment = .right
        return label
    }()
    
    var leftLabel: UILabel = {
        let label :UILabel = UILabel()
        label.font = UIFont.systemFont(ofSize: CGFloat(12))
        label.textAlignment = .left
        return label
    }()
    
    var line: UIView = {
        let view:UIView = UIView()
        view.backgroundColor = UIColor.gray
        return view
    }()
    var closeBtn:UIButton = {
        let view:UIButton = UIButton()
        view.setBackgroundImage(UIImage(named: "guanbi"), for: .normal)
        return view
    }()
    
    convenience init() {
        self.init(frame: CGRect.zero)
    }
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        addSubview(titleLabel)
        addSubview(leftLabel)
        addSubview(rightLabel)
        addSubview(line)
        addSubview(closeBtn)
        
        closeBtn.addTarget(self, action: #selector(btnAction), for: .touchUpInside)
        
        titleLabel.snp.makeConstraints { (make) in
            make.top.equalTo(self).offset(5)
            make.centerX.equalTo(self)
            make.left.equalTo(self).offset(50)
            make.right.equalTo(self).offset(-50)
        }
        leftLabel.snp.makeConstraints { (make) in
            make.bottom.equalTo(line).offset(-5)
            make.left.equalTo(self).offset(5)
        }
        rightLabel.snp.makeConstraints { (make) in
            make.bottom.equalTo(leftLabel)
            make.right.equalTo(self).offset(-5)
        }
        line.snp.makeConstraints { (make) in
            make.bottom.equalTo(self).offset(-5)
            make.right.equalTo(self)
            make.left.equalTo(self)
            make.height.equalTo(1)
        }
        
        closeBtn.snp.makeConstraints { (make) in
            make.top.equalTo(self).offset(5)
            make.right.equalTo(self).offset(-5)
            make.width.equalTo(30)
            make.height.equalTo(30)
        }
        
        
        
    }
    @objc func btnAction() {
        if let clo = self.btnClosure {
            clo()
        }
        
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
}
